﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class admin_edconsultingdetail : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        connection con = new connection();
        SqlConnection cn = con.connect();
        SqlCommand mySqlSelect = new SqlCommand("select * from ConsultingDetails where id = @id", cn);
        SqlParameter paramUserName = new SqlParameter("@id", SqlDbType.BigInt);
        paramUserName.Value = Request.Params["id"];
        mySqlSelect.Parameters.Add(paramUserName);
        mySqlSelect.CommandType = CommandType.Text;
        try
        {
            SqlDataReader rd = mySqlSelect.ExecuteReader();
            if (rd.HasRows)
            {
                rd.Read();
                name.Text = rd.GetSqlString(1).ToString();
                add.Text = rd.GetSqlString(2).ToString();
                phone.Text = rd.GetSqlString(3).ToString();
                email.Text = rd.GetSqlString(4).ToString();
            }
            rd.Close();
            cn.Close();
        }
        catch (Exception ex)
        {
            cn.Close();
            Console.WriteLine("Loi setValueFromDatabaseToForm : " + ex);
        }
    }

    protected void addbt_Click(object sender, EventArgs e)
    {
        info.Text = "";
        try
        {
            connection con = new connection();
            SqlConnection cn = con.connect();
            SqlCommand mySqlSelect = new SqlCommand("update ConsultingDetails set name = @name, address = @add, phone = @ph, email = @email, imageUrl = @img where id = @id", cn);
            mySqlSelect.CommandType = CommandType.Text;
            SqlParameter na = new SqlParameter("@name", SqlDbType.NVarChar, 50);
            na.Value = name.Text;
            SqlParameter addr = new SqlParameter("@add", SqlDbType.NVarChar, 150);
            addr.Value = add.Text;
            SqlParameter ph = new SqlParameter("@ph", SqlDbType.NVarChar, 50);
            ph.Value = phone.Text;
            SqlParameter em = new SqlParameter("@email", SqlDbType.NVarChar, 50);
            em.Value = email.Text;
            SqlParameter id = new SqlParameter("@id", SqlDbType.BigInt);
            id.Value = Request.Params["id"];

            SqlParameter url = new SqlParameter("@img", SqlDbType.NVarChar, 150);
            String regex = "";
            String picPath = "";
            if (img.PostedFile.ContentLength > 2097151)
            {
                info.Visible = true;
                info.Text = "Tập Tin Hình Ảnh Có Dung Lượng Nhỏ Hơn 2MB!";
                return;
            }
            else
            {
                regex = System.IO.Path.GetExtension(img.PostedFile.FileName.ToLower().ToString());
                picPath = "/image/" + Request.Params["id"] + regex;
                if (regex == ".bmp" || regex == ".jpg" || regex == ".gif" || regex == ".png")
                {
                    Common.resizeImage(Server.MapPath("~" + picPath), 120, 80, img);
                }
                else
                {
                    info.Visible = true;
                    info.Text = "Tập Tin Hình Ảnh Có Phần Mở Rộng là .jpg,.bmp,.gif,.png!";
                }
            }
            if (regex == "")
            {
                url.Value = null;
            }
            else
            {
                url.Value = Request.Params["id"] + regex;
            }

            if (url.Value == null)
            {
                mySqlSelect = new SqlCommand("update ConsultingDetails set name = @name, address = @add, phone = @ph, email = @email where id = @id", cn);
                mySqlSelect.Parameters.Add(na);
                mySqlSelect.Parameters.Add(addr);
                mySqlSelect.Parameters.Add(ph);
                mySqlSelect.Parameters.Add(em);
                mySqlSelect.Parameters.Add(id);
            }
            else
            {
                mySqlSelect.Parameters.Add(na);
                mySqlSelect.Parameters.Add(addr);
                mySqlSelect.Parameters.Add(ph);
                mySqlSelect.Parameters.Add(em);
                mySqlSelect.Parameters.Add(id);
                mySqlSelect.Parameters.Add(url);
            }
            mySqlSelect.ExecuteNonQuery();
            cn.Close();
            info.Visible = true;
            info.Text = "Bạn đã cập nhật thành công";
        }
        catch (Exception ex)
        {
            info.Text = "Có lỗi xảy ra trong quá trình nhập dữ liệu";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?page=qlconsultingdetail&id="+Request.QueryString["cid"]);
    }
}